package fm.dongman.animefm.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import fm.dongman.animefm.R;

import java.util.ArrayList;
import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.animefm.view.util.ActivityHelper;
import fm.dongman.modellib.model.ComicModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 首页独家推荐适配器
 * Created by Administrator on 2017/10/12.
 */

public class HomeAnimeRecommendAdapter extends RecyclerView.Adapter<HomeAnimeRecommendAdapter.ViewHolder> {

    private Context mContext;
    private List<IModel.IComicModel> mListRecomm = new ArrayList<>();

    public HomeAnimeRecommendAdapter(List<IModel.IComicModel> list, Context context) {
        this.mContext = context;
        this.mListRecomm = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_home_comic_recommend, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final ComicModel comicModel = (ComicModel) mListRecomm.get(position);

        holder.mTitle.setText(comicModel.getTitle());
        holder.mNum.setText(String.valueOf(position+1));
        Glide.with(mContext).load(comicModel.getImgUrl())
                .transform(new CenterCrop(mContext),new GlideRoundTransform(mContext,2))
                .placeholder(R.mipmap.anime_place_holder)
                .into(holder.mImgUrl);

        //单击事件
        holder.RecommendView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mContext instanceof Activity){
                    Intent intent = new Intent(mContext, SingleActivity.class);
                    intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_ANIME_INFO());
                    intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), comicModel.getId());
                    ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                }else{
                    throw new RuntimeException(mContext.toString()+"必须是Activity!");
                }
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImgUrl; //图片地址
        private TextView mTitle;  //标题
        private TextView mNum;
        private View RecommendView; //view

        public ViewHolder(View itemView) {
            super(itemView);
            mImgUrl = (ImageView) itemView.findViewById(R.id.mIvComicPic);
            mTitle = (TextView) itemView.findViewById(R.id.myTvComicTitle);
            mNum= (TextView) itemView.findViewById(R.id.myTvComicNum);
            RecommendView = itemView;
        }
    }

    @Override
    public int getItemCount() {
        return mListRecomm.size();
    }
}
